package com.kkwrite.regimen.provider.service.review;

import com.kkwrite.regimen.common.constant.DeletedEnum;
import com.kkwrite.regimen.common.entity.dto.order.OrderReviewQueryOutDTO;
import com.kkwrite.regimen.common.entity.dto.order.OrderReviewSubmitInDTO;
import com.kkwrite.regimen.common.entity.dto.picture.PictureDTO;
import com.kkwrite.regimen.provider.domain.RegOrderReviewDO;
import com.kkwrite.regimen.provider.domain.RegOrderReviewLabelLinkDO;
import com.kkwrite.regimen.provider.domain.RegPictureDO;
import com.kkwrite.regimen.provider.exception.RegimenProviderException;
import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.repository.RegOrderReviewLabelLinkRepository;
import com.kkwrite.regimen.provider.repository.RegOrderReviewRepository;
import com.kkwrite.regimen.provider.repository.RegPictureRepository;
import com.kkwrite.regimen.provider.service.picture.PictureService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by lisha on 2018/7/24 10:57.
 *
 * @author lisha
 */
@Service("reviewService")
public class ReviewServiceImpl implements ReviewService {

    @Resource(name = "regOrderReviewRepository")
    private RegOrderReviewRepository orderReviewRepository;
    @Resource(name = "regOrderReviewLabelLinkRepository")
    private RegOrderReviewLabelLinkRepository orderReviewLabelLinkRepository;
    @Resource(name = "pictureService")
    private PictureService pictureService;
    @Resource(name = "regPictureRepository")
    private RegPictureRepository pictureRepository;

    @Override
    public List<OrderReviewQueryOutDTO> findByOrderCode(String orderCode) throws ServiceException {
        List<OrderReviewQueryOutDTO> outList = new ArrayList<>();
        RegOrderReviewDO example = new RegOrderReviewDO();
        example.setOrderCode(orderCode);
        List<RegOrderReviewDO> reviewList = orderReviewRepository.findAll(Example.of(example));
        if (reviewList.isEmpty()) {
            throw new ServiceException("该订单未评价");
        }
        reviewList.parallelStream().forEach((review) -> {
            OrderReviewQueryOutDTO out = new OrderReviewQueryOutDTO();
            BeanUtils.copyProperties(review, out);
            out.setReviewTime(review.getGmtCreate().toLocalDateTime());
            RegOrderReviewLabelLinkDO labelLinkExample = new RegOrderReviewLabelLinkDO();
            labelLinkExample.setOrderCode(orderCode);
            List<RegOrderReviewLabelLinkDO> linkList = orderReviewLabelLinkRepository.findAll(Example.of(labelLinkExample));
            if (!linkList.isEmpty()) {
                List<String> labelList = new ArrayList<>();
                linkList.parallelStream().forEach((link) -> labelList.add(link.getReviewLabelName()));
                out.setReviewLabels(labelList);
            }
            try {
                List<PictureDTO> pictures = pictureService.getOwerPictures(Long.valueOf(review.getReviewId()), (short) 3);
                if (pictures != null && !pictures.isEmpty()) {
                    List<String> pathList = new ArrayList<>();
                    pictures.parallelStream().forEach((picture) -> pathList.add(picture.getPictureUrl()));
                    out.setImagePaths(pathList);
                }
            } catch (Exception e) {
                throw new ServiceException("服务异常！");
            }
            outList.add(out);
        });
        return outList;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void save(final OrderReviewSubmitInDTO reviewData) {
        RegOrderReviewDO reviewDO = new RegOrderReviewDO();
        BeanUtils.copyProperties(reviewData, reviewDO);
        reviewDO.setDeleted(DeletedEnum.UN_DELETED.getValue());
        orderReviewRepository.save(reviewDO);
        if (reviewDO.getReviewId() == null) {
            throw new RegimenProviderException("保存订单评价信息评价表失败");
        }
        final String[] reviewLabels = reviewData.getReviewLabels();
        if (reviewLabels != null && reviewLabels.length > 0) {
            String orderCode = reviewData.getOrderCode();
            int source = reviewData.getSource();
            short unDeletedValue = DeletedEnum.UN_DELETED.getValue();
            Arrays.stream(reviewLabels).parallel().forEach((reviewLabel) -> {
                RegOrderReviewLabelLinkDO reviewLabelLinkDO = new RegOrderReviewLabelLinkDO();
                reviewLabelLinkDO.setOrderCode(orderCode);
                reviewLabelLinkDO.setDeleted(unDeletedValue);
                reviewLabelLinkDO.setReviewLabelName(reviewLabel);
                reviewLabelLinkDO.setSource(source);
                orderReviewLabelLinkRepository.save(reviewLabelLinkDO);
                if (reviewLabelLinkDO.getId() == null) {
                    throw new RegimenProviderException("保存订单评价标签失败");
                }
            });
        }
        String[] imagePaths = reviewData.getImagePaths();
        if (imagePaths != null && imagePaths.length > 0) {
            short sort = 0;
            for (String imagePath : imagePaths) {
                RegPictureDO pictureDO = new RegPictureDO();
                pictureDO.setPictureOwnerId(Long.valueOf(reviewDO.getReviewId()));
                pictureDO.setPictureOwnerType((short) 3);
                pictureDO.setPictureName("评价图");
                pictureDO.setPictureType((short) 11);
                pictureDO.setPictureNote("评价图");
                pictureDO.setDeleted(DeletedEnum.UN_DELETED.getValue());
                pictureDO.setPictureOrder(++sort);
                pictureDO.setPictureUrl(imagePath);
                pictureRepository.save(pictureDO);
                if (pictureDO.getPictureId() == null) {
                    throw new RegimenProviderException("评价图保存失败");
                }
            }
        }
    }

}
